// 引入 gulp
var gulp = require('gulp'); 

// 引入组件
var jshint = require('gulp-jshint');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var clean = require('gulp-clean');
var csso = require('gulp-csso');
var rev = require('gulp-rev');
var replace = require('gulp-replace');


var basePath = 'http://localhost:8881';


// 删除发布后的文件
gulp.task('clean', function() {  
  return gulp.src(['dist'], {read: false})
    .pipe(clean());
});

//拷贝js文件
gulp.task('copyJs', function() {
    gulp.src(['app/js/**'])
        .pipe(replace(/\$\{basePath\}/g, basePath))
        .pipe(gulp.dest('dist/js'));
});
//拷贝img文件
gulp.task('copyImg', function() {
    gulp.src('app/images/**')
        .pipe(gulp.dest('dist/images'));
});
//拷贝fonts文件
gulp.task('copyFonts', function() {
    gulp.src('app/fonts/**')
         .pipe(replace(/\$\{basePath\}/g, basePath))
        .pipe(gulp.dest('dist/fonts'));
});

//拷贝css文件
gulp.task('copyCss', function() {
    gulp.src('app/css/**')
        .pipe(replace(/\$\{basePath\}/g, basePath))
        .pipe(gulp.dest('dist/css'));
});
//拷贝html文件
gulp.task('copyHtml', function() {
    gulp.src(['app/views/**'])
        .pipe(replace(/\$\{basePath\}/g, basePath))
        .pipe(gulp.dest('dist/views'));
     gulp.src(['app/*.html'])
        .pipe(replace(/\$\{basePath\}/g, basePath))
        .pipe(gulp.dest('dist'));
});

//拷贝video文件
gulp.task('copyVideo', function() {
    gulp.src(['app/video/**'])
        .pipe(gulp.dest('dist/video'));
});

gulp.task('copyPlugins', function() {
    gulp.src(['app/plugins/**'])
        .pipe(gulp.dest('dist/plugins'));
});

//拷贝tmp文件
gulp.task('copyTmp',function(){
    gulp.src('app/tmp/**')
        .pipe(gulp.dest('dist/tmp'));
});



//需要执行的任务
var tasks =['copyJs','copyCss','copyHtml','copyImg','copyFonts','copyVideo','copyPlugins','copyTmp'];

//deploy
gulp.task('deploy' , function(){
        gulp.run(tasks);
});


// 默认任务
gulp.task('default', function(){

    gulp.run(tasks);

     // 监听js文件变化
    gulp.watch('app/js/**', function(){
        gulp.run('copyJs');
    });
     // 监听css文件变化
    gulp.watch('app/css/**', function(){
        gulp.run('copyCss');
    });
     // 监听html文件变化
    gulp.watch(['app/views/**','app/*.html'], function(){
        gulp.run('copyHtml');
    });
    // 监听img文件变化
    gulp.watch(['app/images/**'], function(){
        gulp.run('copyImg');
    });
     // 监听fonts文件变化
    gulp.watch(['app/fonts/**'], function(){
        gulp.run('copyFonts');
    });
     // 监听video文件变化
    gulp.watch(['app/video/**'], function(){
        gulp.run('copyVideo');
    });
    //监听plugins文件变化
    gulp.watch('app/plugins/**',function(){
        gulp.run('copyPlugins');
    });
    //监听tmp文件变化
    gulp.watch('app/tmp/**',function(){
        gulp.run( 'copyTmp');
    });
});